module("UI.Windows.WebViewKefuWindow", package.seeall)

local NetMgr = require("NetWork").NetMgr
local UIWinMgr = require("UI.UIWinMgr").UIWinMgr
local UIUtility = require("UI.Utility")
local atools = require("StringTool").StringTool
local UIWindow = require("UI.UIWindow").UIWindow
local GameObject = UnityEngine.GameObject
WebViewKefuWindow = UIWindow:new()
WebViewKefuWindow.name = "WebViewKefuWindow"


function WebViewKefuWindow:Init()
    local t = self.Container.transform

    self.CanvasWebViewPrefab = UIUtility.GetChildTransform("CanvasWebViewPrefab", t, true)
    --self.CanvasWebViewPrefab = UIUtility.GetChildTransform("CanvasWebViewPrefab", t, true)
    self.data_info = UIUtility.GetChildTransform("data_info", t, true)
    self.data1 = self.data_info.transform:GetChild(0)
    self.close_btn = UIUtility.GetChildTransform("close_btn", t, true)

    --1 客服  3瓦力  4bbin 5we
    self.gamePlatformId = 0
    --1 视讯 2捕鱼 3棋牌 4 电子 5体育 6 彩票
    self.gameType = 0



    self:BindEvents()

    local isHit = false
    self.downCall = {}
    UIHelper.BindUIEvnet("Click", function()
        if isHit == true then
            print("重复点击")
            return
        end
        isHit = true


        CountDownMgr.Instance:CreateCountDown(3, 3, function()
            isHit = false
        end)
        self:CloseWin()
    end, UIUtility.GetChildTransform("close", t, true).gameObject)
end

function WebViewKefuWindow:OnClose()

end

--https://9med42zz.ddwkx.cn/video/?uid=178223056&game=80&token=MTc4MjIzMDU2XzE3NDk4ODI1NzM3Mjg6WnhNaU1ZYWIzWVlqMlM3eg
--如果gamePlatformId 1  则无需判断，直接给位置 并显示
--如果gamePlatformId 2  则判断url   gameTpe 为1 为竖屏
function WebViewKefuWindow:urlChanged()
    -- local tempUrlArr = atools:Split(self.data1.gameObject.name, "serverId")
    -- if self.gamePlatformId == 1 then
    --     self.CanvasWebViewPrefab.gameObject:SendMessage("SetBtnPos", "0_1_0_1_0_1_0_0_0_150_-20")
    --     self.close_btn.gameObject:SetActive(true)
    -- elseif self.gamePlatformId == 3 then
    --     if self.gameType == 1 then
    --         --通过?id= 判断是否在视讯游戏里
    --         tempUrlArr = atools:Split(self.data1.gameObject.name, "?id=")
    --         self.close_btn.gameObject:SetActive(#tempUrlArr == 1)
    --         --位置设置到 左上
    --         self.CanvasWebViewPrefab.gameObject:SendMessage("SetBtnPos", "0_1_0_1_0_1_0_0_0_150_-20")
    --     else
    --         self.close_btn.gameObject:SetActive(true)
    --         --位置设置到 左上
    --         self.CanvasWebViewPrefab.gameObject:SendMessage("SetBtnPos", "0_1_0_1_0_1_0_0_0_150_-40")
    --     end
    -- elseif self.gamePlatformId == 4 then
    --     if self.gameType == 1 then
    --         tempUrlArr = atools:Split(self.data1.gameObject.name, "&HALLID=")
    --         self.close_btn.gameObject:SetActive(#tempUrlArr > 1)
    --         --位置设置到 左上
    --         self.CanvasWebViewPrefab.gameObject:SendMessage("SetBtnPos", "0_1_0_1_0_1_0_0_0_150_-40")
    --     else
    --         self.close_btn.gameObject:SetActive(true)
    --         --位置设置到 左上
    --         self.CanvasWebViewPrefab.gameObject:SendMessage("SetBtnPos", "0_1_0_1_0_1_0_0_0_150_-40")
    --     end
    -- elseif self.gamePlatformId == 5 then
    --     if self.gameType == 1 then
    --         tempUrlArr = atools:Split(self.data1.gameObject.name, "&HALLID=")
    --         self.close_btn.gameObject:SetActive(#tempUrlArr > 1)
    --         --位置设置到 左上
    --         self.CanvasWebViewPrefab.gameObject:SendMessage("SetBtnPos", "0_1_0_1_0_1_0_0_0_150_-40")
    --     else
    --         self.close_btn.gameObject:SetActive(true)
    --         --位置设置到 左上
    --         self.CanvasWebViewPrefab.gameObject:SendMessage("SetBtnPos", "0_1_0_1_0_1_0_0_0_150_-40")
    --     end
    -- end
end

function WebViewKefuWindow:CloseWin()
    --客服無需下分
    if self.gamePlatformId == 1 then
        self:SetAudioMuted(true)
        self.CanvasWebViewPrefab.gameObject:SetActive(false)
        self:Close()
        self:Destroy()
        local setSound = UnityEngine.PlayerPrefs.GetString("SetSound")
        local SoundModule = require("Module.SoundModule").SoundModule
        if setSound == "1" then
            SoundModule:SetAllBgVolume(0.2)
        else
            SoundModule:SetAllBgVolume(0)
        end
    else
        require("UI.Windows.NewGameLoobyWin").NewGameLoobyWin:xiaFenUpdate(function()
            self:SetAudioMuted(true)
            self.CanvasWebViewPrefab.gameObject:SetActive(false)
            self:Close()
            self:Destroy()
            local setSound = UnityEngine.PlayerPrefs.GetString("SetSound")
            local SoundModule = require("Module.SoundModule").SoundModule
            if setSound == "1" then
                SoundModule:SetAllBgVolume(0.2)
            else
                SoundModule:SetAllBgVolume(0)
            end
        end)
    end
end

function WebViewKefuWindow:BindEvents()

end

function WebViewKefuWindow:SetUrl(url)
    if self.CanvasWebViewPrefab then
        self.CanvasWebViewPrefab.gameObject:SetActive(true)
        self.CanvasWebViewPrefab.gameObject:SendMessage("LoadUrl", url)
    else

    end
end

function WebViewKefuWindow:SetHtml(html)
    if self.CanvasWebViewPrefab then
        self.CanvasWebViewPrefab.gameObject:SetActive(true)
        self.CanvasWebViewPrefab.gameObject:SendMessage("LoadHtml",html)
    else

    end
end

function WebViewKefuWindow:SetAudioMuted(muted)
    if self.CanvasWebViewPrefab then
        self.CanvasWebViewPrefab.gameObject:SendMessage("SetAudioMuted", muted)
    end
end

function WebViewKefuWindow:OnShow()
    self.close_btn.gameObject:SetActive(false)
    CountDownMgr.Instance:CreateCountDown(0.2, 0.2, function()
        UIWinMgr:CloseMask()
    end)
end
